package org.korecky.gnotedigitalorganizer;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.io.File;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import net.miginfocom.swing.MigLayout;
import org.korecky.gnotedigitalorganizer.threads.DetectNewDeviceThread;
import org.korecky.gnotedigitalorganizer.threads.DetectNewDeviceThread.NewDeviceDetectedEvent;
import org.korecky.gnotedigitalorganizer.threads.DetectNewDeviceThread.NewDeviceDetectedEventListener;

public class NewDeviceWindow extends JFrame {
    private JPanel contentPane;
    private JTextPane txtpnComment1stStep;
    private JPanel pnl1stStep;
    private JLabel lblNewLabel;
    private JPanel panel_1;
    private JTextPane txtpnSwitchOn;
    private JLabel label;

    /**
     * Create the frame.
     */
    public NewDeviceWindow() {
	SwingUtilities.invokeLater(new Runnable() {
	    @Override
	    public void run() {
		initComponents();
	    }
	});

	// Run thread for detecting new device
	DetectNewDeviceThread newDeviceThread = new DetectNewDeviceThread("newDeviceThread");
	newDeviceThread.addNewDeviceDetectedEventListener(new NewDeviceDetectedEventListener() {
	    @Override
	    public void newDeviceDetectedEventOccurred(NewDeviceDetectedEvent evt) {
		DetectNewDeviceThread newDeviceThread = (DetectNewDeviceThread) evt.getSource();
		newDeviceThread.finish();
		List<File> newDevices = newDeviceThread.getNewDevices();
		for (File newDevice : newDevices) {
		    ApproveNewDeviceWindow approveDeviceWindow = new ApproveNewDeviceWindow(newDevice);
		    approveDeviceWindow.setVisible(true);
		}
		dispose();
	    }
	});
	newDeviceThread.start();
    }

    private void initComponents() {
	setResizable(false);
	setIconImage(Toolkit.getDefaultToolkit().getImage(NewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons128x128/usb.png")));
	setTitle("Add new device");
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setBounds(100, 100, 665, 407);
	this.contentPane = new JPanel();
	this.contentPane.setBackground(SystemColor.window);
	this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(this.contentPane);
	this.contentPane.setLayout(new MigLayout("", "[fill][fill]", "[grow,fill]"));

	this.panel_1 = new JPanel();
	this.panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
	this.panel_1.setBackground(Color.WHITE);
	this.contentPane.add(this.panel_1, "cell 0 0,grow");
	this.panel_1.setLayout(new MigLayout("", "[grow,fill]", "[grow][grow,fill]"));

	this.txtpnComment1stStep = new JTextPane();
	this.panel_1.add(this.txtpnComment1stStep, "cell 0 0");
	this.txtpnComment1stStep.setFont(new Font("Tahoma", Font.ITALIC, 11));
	this.txtpnComment1stStep.setText("1) Disconnect your G-Note device from your computer, if is connected.");
	this.txtpnComment1stStep.setBackground(Color.WHITE);
	this.txtpnComment1stStep.setEditable(false);

	this.label = new JLabel("");
	this.label.setIcon(new ImageIcon(NewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/others/disconnect-computer-pc.jpg")));
	this.label.setHorizontalAlignment(SwingConstants.CENTER);
	this.panel_1.add(this.label, "flowy,cell 0 1");

	this.pnl1stStep = new JPanel();
	this.pnl1stStep.setBorder(new LineBorder(new Color(0, 0, 0)));
	this.contentPane.add(this.pnl1stStep, "cell 1 0");
	this.pnl1stStep.setBackground(Color.WHITE);
	this.pnl1stStep.setLayout(new MigLayout("", "[grow,fill]", "[grow][grow,fill]"));

	this.txtpnSwitchOn = new JTextPane();
	this.txtpnSwitchOn.setText("2) Switch on your G-Note device and connect it to the USB port of your computer again.");
	this.txtpnSwitchOn.setFont(new Font("Tahoma", Font.ITALIC, 11));
	this.txtpnSwitchOn.setEditable(false);
	this.txtpnSwitchOn.setBackground(Color.WHITE);
	this.pnl1stStep.add(this.txtpnSwitchOn, "cell 0 0");

	this.lblNewLabel = new JLabel("");
	this.lblNewLabel.setBackground(Color.WHITE);
	this.lblNewLabel.setIcon(new ImageIcon(NewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/others/connect-computer-pc.jpg")));
	this.lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
	this.pnl1stStep.add(this.lblNewLabel, "flowy,cell 0 1");
    }
}
